<?php
/*
 * 达人样品订单
 */

namespace app\adminbackstage\controller;
use app\BaseController;
use think\facade\Request;
use app\adminbackstage\model\SampleOrder;

use app\adminbackstage\model\Product;
use app\adminbackstage\model\InventoryTotal;
use think\facade\Db;

class SampleOrderController extends BaseController{

    //达人样品订单列表
    public function sampleOrderList(){
        $a_uid = Request::param('a_uid');
        $page = Request::param('page');
        $num = Request::param('num');
        
        $s_name = Request::param('s_name');
        $p_name = Request::param('p_name');
        $o_pro_num = Request::param('o_pro_num');
        $p_num = Request::param('p_num');
        $where = [];
        $where[] = ['enable','=','1'];
        if(!empty($s_name)){
            $where[] = ['s_name','like','%'.$s_name.'%'];
        }
        
        if(!empty($p_name)){
            $where[] = ['p_name','like','%'.$p_name.'%'];
        }
        if(!empty($o_pro_num)){
            $where[] = ['o_pro_num','=',$o_pro_num];
        }
        if(!empty($p_num)){
            $where[] = ['p_num','=',$p_num];
        }
        $list = SampleOrder::sampleOrderList($where,$page,$num);
        if(!empty($list)){
            $list = $list->toArray();
            foreach ($list as $key => $value) {
                $list[$key]["addtime"] = date( "Y-m-d H:i:s",$value["addtime"]);
                if(isset($value["paytime"])){
                    $list[$key]["paytime"] = date( "Y-m-d H:i:s",$value["paytime"]);
                }
                if(isset($value["sendtime"])){
                    $list[$key]["sendtime"] = date( "Y-m-d H:i:s",$value["sendtime"]);
                }
                if(isset($value["endtime"])){
                    $list[$key]["endtime"] = date( "Y-m-d H:i:s",$value["endtime"]);
                }
                    /*if(strpos($value['img'],"img.alicdn.com") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    elseif(strpos($value['img'],"http") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    else{
                        
                        $storeList[$key]["img"] =  Config::get('app.swimming_url').$value['img'];
                    }*/
            }
        }
        $count = SampleOrder::sampleOrderCount($where);
        return json(['code' => 1, 'msg' => lang(''), 'data' => $list, 'count' => $count]);
    }
    
        //发货
    public function sendOutGoods(){
        $a_uid = Request::param('a_uid');
        $id = Request::param('id');
        $w_id = Request::param('wid');
        $state = Request::param('state');//0未付款 1待发货 2发货中 3收货 -1已取消
        $update = [];
        if($state>0 && $state !=2){
            $update['state']= $state;
        }
        elseif($state==2){
            $update['state']= $state;
            $where = [];
            $where[] = ['id','=',$id];

            $info = SampleOrder::sampleOrderInfo($where);
            if($info['tinyint']==2){
                return json(['code' => 0, 'msg' => lang('该订单缺货'), 'data' => '', 'count' => '' ]);
            }
            $where_i = [];
            $where_i[] = ['w_id','=',$w_id];
            $where_i[] = ['p_id','=',$info['p_id']];
            $where_i[] = ['enable','=',1];
            
            $inventoryTotalInfo = InventoryTotal::inventoryTotalInfo($where_i);//总库存
            
            if(!empty($inventoryTotalInfo)){
                if(($inventoryTotalInfo['ware_quantity']-$inventoryTotalInfo['sold'])>=$info['order_quantity']){//库存足够

                    Db::startTrans();
                    try {
                        $time = time();
                        $update['sup_quantity']= $info['order_quantity'];
                        SampleOrder::sampleOrderEdit($id,$update);
                        
                        $sql = 'update wa_product set sold=sold+'.$info['order_quantity'].' where id='.$info['p_id'] ;
                        Product::ruleSql($sql);
                        
                        $sql_t = 'update wa_inventory_total set sold=sold+'.$info['order_quantity'].' where id='.$inventoryTotalInfo['id'] ;
                        Product::ruleSql($sql_t);


                        Db::commit();
                    }catch (\Exception $e){
                        Db::rollback();
                    }
                }
                else{
                    
                    
                    
                    return json(['code' => 0, 'msg' => lang('库存不足,请先添加'), 'data' => '', 'count' => '' ]);
                }
            }
            else{
                return json(['code' => 0, 'msg' => lang('无库存,请先添加'), 'data' => '', 'count' => '' ]);
            }
         

            
        }elseif($state<0){
            $update['state']= $state;
        }
        $info = SampleOrder::sampleOrderEdit($id,$update);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '' ]);
    }
    
    //仓库添加
    /*public function warehouseAdd(){
        $store = new Store();
        $a_uid = Request::param('a_uid');
        $enable = Request::param('enable');
        $w_name = Request::param('w_name');
        $type = Request::param('type');
        $province = Request::param('province');
        $city = Request::param('city');
        $district = Request::param('district');
        $address = Request::param('address');
        
        if(empty($enable)){
            $enable = 1;
        }
        if($enable == -1){
            $enable  = 0;
        }
        $time = time();
        $data = [

            'w_name' => $w_name,
            'enable' => $enable,
            'addtime' => $time,
            'type' => $type,
            'province' => $province,
            'city' => $city,
            'address' => $address,
            'district' => $district,
            'address' => $address,

        ];

        Warehouse::warehouseAdd($data);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }*/
    
    //达人样品订单详情
    public function sampleOrderInfo(){
        $a_uid = Request::param('a_uid');

        $id = Request::param('id');
  
        $where = [];
        $where[] = ['id','=',$id];

        $info = SampleOrder::sampleOrderInfo($where);
        if(!empty($info)){
            $info = $info->toArray();

            $info["addtime"] = date( "Y-m-d H:i:s",$info["addtime"]);
            
            if(isset($info["paytime"])){
                $info["paytime"] = date( "Y-m-d H:i:s",$info["paytime"]);
            }
            if(isset($info["sendtime"])){
                $info["sendtime"] = date( "Y-m-d H:i:s",$info["sendtime"]);
            }
            if(isset($info["endtime"])){
                $info["endtime"] = date( "Y-m-d H:i:s",$info["endtime"]);
            
            }

            /*if(strpos($storeList['img'],"img.alicdn.com") !== false){
                $storeList["img"] =  $storeList['img'];
            }
            elseif(strpos($storeList['img'],"http") !== false){
                $storeList["img"] =  $storeList['img'];
            }
            else{

                $storeList["img"] =  Config::get('app.swimming_url').$storeList['img'];
            }*/
       
        }

        return json(['code' => 1, 'msg' => lang(''), 'data' => $info, 'count' => '' ]);
    }
    
    //达人样品订单修改
    public function sampleOrderEdit(){

        /*$a_uid = Request::param('a_uid');
        $id = Request::param('id');
        $enable = Request::param('enable');
        $w_name = Request::param('w_name');
        $type = Request::param('type');
        $province = Request::param('province');
        $city = Request::param('city');
        $district = Request::param('district');
        $address = Request::param('address');
        
        $update = [];
        if(!empty($w_name)){
            $update['w_name'] = $w_name;
        }
        
        if(!empty($type)){
            $update['type'] = $type;
        }
        
        if(!empty($province)){
            $update['province'] = $province;
        }
        
        if(!empty($city)){
            $update['city'] = $city;
        }
        
        if(!empty($district)){
            $update['district'] = $district;
        }
        
        if(!empty($address)){
            $update['address'] = $address;
        }
        
        if($enable==-1){
            $update['enable'] = 0;
        }else{
            $update['enable'] = 1;
        }


        Warehouse::warehouseEdit($id,$update);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);*/
    }

}
